package com.kmzx.one.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kmzx.one.common.exceptions.NoSuchObjectException;
import com.kmzx.one.project.entity.RankTab;
import com.kmzx.one.project.entity.UserInvite;
import com.kmzx.one.project.mapper.RankTabMapper;
import com.kmzx.one.project.mapper.UserInviteMapper;
import com.kmzx.one.project.mapper.UserRankMapper;
import com.kmzx.one.project.others.dso.UserRankDso;
import com.kmzx.one.project.others.dto.FansInfo;
import com.kmzx.one.project.others.dto.UserRankInfo;
import com.kmzx.one.project.others.dto.UserRankMoreInfo;
import com.kmzx.one.project.service.UserFanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserFanServiceImpl implements UserFanService {

    @Autowired
    private UserRankMapper userRankMapper;
    @Autowired
    private UserInviteMapper userInviteMapper;
    @Autowired
    private RankTabMapper rankTabMapper;

    @Override
    public UserRankInfo findUserRankTabs(String userId) {
        UserRankInfo rankTabs = userRankMapper.selectUserRankTabs(userId);
        return rankTabs;
    }

    @Override
    public List<FansInfo> findUserFans(String userId, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<FansInfo> pager = userInviteMapper.selectUserFans(userId);
        List<FansInfo> result = pager.getResult();
        if (!CollectionUtils.isEmpty(result)) {
            for (FansInfo fansInfo : result) {
                int num = userInviteMapper.selectUserInviteNum(fansInfo.getUserId());
                fansInfo.setChild(num);
            }
            List<RankTab> tabs = rankTabMapper.selectAllRankTab();
            setNextNameValue2(result,tabs);
        }
        return result;
    }

    @Override
    public List<FansInfo> findUserFansPagers(String userId, Integer pageNum, Integer pageSize, Integer kinds) throws Exception {
        List<FansInfo> list =  haveNext(userId);
        List<FansInfo> list1 = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            if(kinds-1 == list.get(i).getLevels()){
                list1.add(list.get(i));
            }
        }
//        List<FansInfo> fansInfos = list1.subList((pageNum - 1) * pageSize, pageNum * list1.size());
//        list1 = fansInfos;
        if (!CollectionUtils.isEmpty(list1)) {
            List<RankTab> tabs = rankTabMapper.selectAllRankTab();
            setNextNameValue2(list1,tabs);
        }
//        List<FansInfo> end = list1.stream().distinct().collect(Collectors.toList());
        return list1;
    }

    @Override
    public UserRankMoreInfo findMyManager(String userId) throws Exception {
        UserRankInfo rankTabs = userRankMapper.selectUserRankTabs(userId);
        if(rankTabs!=null){
            int rankNo = rankTabs.getRankNo();
            if(rankNo==3){
                throw new NoSuchObjectException(204,"您已是最高级用户了(#^.^#)");
            }
        }
        List<UserInvite> userInvites = userInviteMapper.selectUserInviteByUserId(userId);
        if (CollectionUtils.isEmpty(userInvites)) {
            return null;
        }
        String fromUser = userInvites.get(0).getFromUser();
        UserRankMoreInfo myManager = userInviteMapper.selectMyManager(fromUser);
        int rankNum = myManager.getRankNum();
        if (rankNum != 3) {
            List<UserRankDso> tabs = userRankMapper.selectAllRankTabs();
            for (int i = 0; i < tabs.size(); i++) {
                if (tabs.get(i).getRankId().equals(myManager.getRankId()) && i != tabs.size() - 1) {
                    myManager.setNextName(tabs.get(i + 1).getRankName());
                    break;
                }
            }
        }
        return myManager;
    }

    @Override
    public List<FansInfo> findUserOlderFans(String userId, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<FansInfo> pager = userInviteMapper.findUserFansOld(userId);
        List<FansInfo> result = pager.getResult();
        if (!CollectionUtils.isEmpty(result)) {
            List<RankTab> tabs = rankTabMapper.selectAllRankTab();
            setNextNameValue2(result,tabs);
//            setNextNameValues(result);
        }
        return result;
    }




    public void setNextNameValue2(List<FansInfo> result,List<RankTab> tabs) {
        HashMap<Integer, BigDecimal> tabss = new HashMap<>();
        HashMap<Integer, String> names = new HashMap<>();
        if (!CollectionUtils.isEmpty(tabs)) {
            for (RankTab tab : tabs) {
                tabss.put(tab.getRankNumber(), tab.getRankPay());
                names.put(tab.getRankNumber(), tab.getRankName());
            }
        }
        for (FansInfo FansInfo : result) {
            int levels = FansInfo.getLevels();
            BigDecimal growths = FansInfo.getGrowths();
            if (levels != 3) {
                BigDecimal bigDecimal = tabss.get(levels);

                BigDecimal needs = bigDecimal.subtract(growths);
                FansInfo.setNextName(names.get(levels + 1));
                FansInfo.setNeeds(needs);
            }
        }
    }

    public void setNextNameValues(List<FansInfo> result) {
        List<RankTab> tabs = rankTabMapper.selectAllRankTab();
        HashMap<Integer, BigDecimal> tabss = new HashMap<>();
        HashMap<Integer, String> names = new HashMap<>();
        if (!CollectionUtils.isEmpty(tabs)) {
            for (RankTab tab : tabs) {
                tabss.put(tab.getRankNumber(), tab.getRankPay());
                names.put(tab.getRankNumber(), tab.getRankName());
            }
        }
        for (FansInfo FansInfo : result) {
            int levels = FansInfo.getLevels();
            BigDecimal growths = FansInfo.getGrowths();
            if (levels != 3) {
                BigDecimal bigDecimal = tabss.get(levels);

                BigDecimal needs = bigDecimal.subtract(growths);
                FansInfo.setNextName(names.get(levels + 1));
                FansInfo.setNeeds(needs);
            }
        }
    }

    public List<FansInfo> haveNext(String userId){
        Page<FansInfo> pager = userInviteMapper.selectUserFans(userId);
        List<FansInfo> result = pager.getResult();
        for(int i =0;i<result.size();i++) {
            List<FansInfo> list1 =  haveNext(result.get(i).getUserId());
            result.addAll(list1);
//            if (result.get(i).getLevels() > 0) {
//            }
        }
        return result;
    }

    @Override
    public List<FansInfo> findUserFansEntry(String userId, Integer pageNum, Integer pageSize) throws Exception {
        Page<FansInfo> pager = userInviteMapper.selectUserFans(userId);
        List<FansInfo> result = pager.getResult();
        if (!CollectionUtils.isEmpty(result)) {
            for (FansInfo fansInfo : result) {
                String selfId = fansInfo.getUserId();
                int num = userInviteMapper.selectUserInviteNum(selfId);
                fansInfo.setChild(num);
            }
            List<RankTab> tabs = rankTabMapper.selectAllRankTab();
            setNextNameValue2(result,tabs);
        }

        return result;
    }

}
